<template>
  <div class="todo-footer" v-show="total">
    <label>
      <!-- <input type="checkbox" :checked="isShow" @change="checkAll"/> -->
      <input type="checkbox" v-model="isShow" />
    </label>
    <span>
      <span>已完成{{ doneTotle }}</span> / 全部{{ total }}
    </span>
    <button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
  </div>
</template>

<script>
export default {
  name: "FooterList",
  props: ["todos"],
  computed: {
    isShow: {
      get() {
        return this.doneTotle === this.total && this.total > 0;
      },
      set(value) {
        // this.checkAllTodo(value);
        this.$emit('checkAllTodo',value);
      },
    },
    total() {
      return this.todos.length;
    },
    doneTotle() {
      // const x = this.todos.reduce((pre, current) => {
      //   console.log('@',pre);
      //   return pre + (current.done ? 1 : 0)
      // }, 0)
      // console.log("x",x);
      // return x
      // reducer 函数接收4个参数:
      // Accumulator (acc) (累计器)
      // Current Value (cur) (当前值)
      // Current Index (idx) (当前索引)
      // Source Array (src) (源数组)
      return this.todos.reduce(
        (acc, current) => acc + (current.done ? 1 : 0),
        0
      );
    },
  },
  methods: {
    checkAll(e) {
      this.checkAllTodo(e.target.checked);
    },
    clearAll() {
      if (confirm("确定删除已经完成的任务吗？")) {
        // this.clearTodoDone();
        this.$emit('clearTodoDone');
      }
    },
  },
};
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>